package com.zxj.camerademo;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.zxj.camerademo.camera1.CameraActivity;
import com.zxj.camerademo.camera2.Camera2Activity;
import com.zxj.camerademo.camera2.Camera2FaceActivity;
import com.zxj.camerademo.utils.PermissionUtils;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final int PERMISSION_REQUEST_CODE = 100;
    private static final int PERMISSION_SETTING_CODE = 101;

    private String[] permissionsList = new String[]{Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
                == PackageManager.PERMISSION_GRANTED) {

            Toast.makeText(this,"您申请了相机动态权限",Toast.LENGTH_SHORT).show();
        }else {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.CAMERA}, 100);
        }

        Button btCapture = findViewById(R.id.btCapture);
        Button btCamera = findViewById(R.id.btCamera);
        Button btCameraRecord = findViewById(R.id.btCameraRecord);
        Button btCamera2 = findViewById(R.id.btCamera2);
        Button btCamera2Face = findViewById(R.id.btCamera2Face);
        Button btTest = findViewById(R.id.btTest);

        btCapture.setOnClickListener(this);
        btCamera.setOnClickListener(this);
        btCameraRecord.setOnClickListener(this);
        btCamera2.setOnClickListener(this);
        btCamera2Face.setOnClickListener(this);
        btTest.setOnClickListener(this);

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case PERMISSION_REQUEST_CODE:
                if (permissions[0].equals(Manifest.permission.CAMERA)){

                    if (grantResults[0]  == PackageManager.PERMISSION_GRANTED){
                        //如果用户同意了再去打开相机
                        Toast.makeText(MainActivity.this,"非常感谢您的同意,再会",Toast.LENGTH_SHORT).show();
//                        startCamera();

                    }else{
                        //因为第一次的对话框是系统提供的 从这以后系统不会自动弹出对话框 我们需要自己弹出一个对话框
                        //进行询问的工作
//                        startAlertDiaLog();
                    }
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == PERMISSION_REQUEST_CODE){
            Log.e("MainActivity","从设置权限页面返回后，重新请求权限");

        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btCapture:
                //系统相机拍照
                startActivity(new Intent(this, CaptureActivity.class));
                break;
            case R.id.btCamera:
                //Camera 拍照
                Intent intent = new Intent(this, CameraActivity.class);
                intent.putExtra(CameraActivity.TYPE_TAG, CameraActivity.TYPE_CAPTURE);
                startActivity(intent);
                break;
            case R.id.btCameraRecord:
                //Camera 录视频
                Intent intentRecord = new Intent(this, CameraActivity.class);
                intentRecord.putExtra(CameraActivity.TYPE_TAG, CameraActivity.TYPE_RECORD);
                startActivity(intentRecord);
                break;
            case R.id.btCamera2:
                //Camera2 拍照
                startActivity(new Intent(MainActivity.this, Camera2Activity.class));
                break;
            case R.id.btCamera2Face:
                //Camera2 人脸检
                startActivity(new Intent(MainActivity.this, Camera2FaceActivity.class));
                break;
            case R.id.btTest:
                //test

                break;

        }
    }
}
